package listenerUtil;


import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceArray;

@Slf4j
public class ControllerStringListener {

 /*   private ListenContainer listenContainer;

    public ControllerListener() {
        this.listenContainer = new ListenContainer();
    }*/

    public static void main(String[] args) throws InterruptedException {

        ListenContainer listenContainer = new ListenContainer();

        //开启监听线程
        //间隔2秒执行一次心跳检测
        new Thread(()->{
            try {
                while (true){
                    log.info("监听中...");
                    Thread.sleep(500);
                    //每间隔两秒监听一次静态容器,有数据就按照顺序输出
                    AtomicReference<String> atomicStringReference = ListenContainer.getStringAtomicReference();
                    if(atomicStringReference != null && atomicStringReference.get() != null){
                        Object obj = atomicStringReference;
                        log.info(obj.toString());
                        //还原数据
                        atomicStringReference.compareAndSet(obj.toString(),null);
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        },"Listener String Thread listening...").start();


        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                listenContainer.addStringElement(String.valueOf(i));
            }

        },"put element").start();
    }

}
